Aug 25, 2023
Each type of view has its own benefits and use cases.
from django.shortcuts import render
def my_view(request):
# Process the request
return render(request, 'template.html', context)Class-based views provide a more organized way to handle view logic by using class inheritance.
CBVs allow you to reuse common behavior using class inheritance, making it easier to manage and extend views with mixins and overrides.
CBVs are suitable when you want to structure your views using class inheritance and take advantage of built-in methods for different HTTP methods (e.g., get, post, etc.).
Generic class-based views are pre-built views provided by Django to handle common patterns, like displaying lists, details, creating, updating, and deleting objects.
They encapsulate common view logic and reduce code repetition.
GCBVs are particularly useful for standard operations like listing objects, displaying details, and performing CRUD operations. They reduce the need to write repetitive code.
The choice between these types of views depends on - the complexity of your view logic, - the need for code organization and reuse, and - the availability of built-in functionality for your specific use case.
A comprehensive example of function-based views, class-based views, and generic views.
We’ll create a simple blog post listing using class based, function based and generic views.
Follow these steps:
viewsdemocbv/views.py, define function-based and class-based views:from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
from django.views.generic import ListView
from .models import BlogPost
def fbv_greet(request):
return HttpResponse("Hello from Function-Based View!")
class CBVGreetView(View):
def get(self, request):
return HttpResponse("Hello from Class-Based View!")
class BlogPostListView(ListView):
model = BlogPost
template_name = 'viewsdemocbv/blogpost_list.html'
context_object_name = 'blogposts'viewsdemocbv/urls.py, set up URL patterns for FBV, CBV, and the generic view:viewsdemocbv app directory, create a templates directory. Inside the templates directory, create a viewsdemocbv subdirectory and add a file named blogpost_list.html:<!DOCTYPE html>
<html>
<head>
<title>{% block title %}ViewsDemo{% endblock %}</title>
</head>
<body>
<header>
<h1><a href="{% url 'fbv_greet' %}">ViewsDemo</a></h1>
<nav>
<ul>
<li><a href="{% url 'fbv_greet' %}">FBV Greet</a></li>
<li><a href="{% url 'cbv_greet' %}">CBV Greet</a></li>
<li><a href="{% url 'blogpost_list' %}">Blog Posts</a></li>
</ul>
</nav>
</header>
<main>
{% block content %}{% endblock %}
</main>
</body>
</html>In cbv_greet.html, use Django template language to display a greeting for the CBV: In blogpost_list.html, use Django template language to display a list of blog posts:
viewsdemocbv/models.py, define a simple BlogPost model:ViewsDemoProjectCBV/urls.py, include the URLs from the viewsdemocbv app:Open your browser and visit -
- http://127.0.0.1:8000/viewsdemocbv/fbv-greet/
- http://127.0.0.1:8000/viewsdemocbv/cbv-greet/
- http://127.0.0.1:8000/viewsdemocbv/blogposts/
Manish Patel